import axios from 'axios';
import qs from 'qs';
import Vue from 'vue';
import Element from 'element-ui'
import router from 'src/router';

/* 登录的请求不使用 */
export const AjaxPost_Login = (url: string, req: MsgReqBase, func: any) => {
    console.log('%c--请求参数--', 'background:#000;color:#bada55');
    console.log(req);
    let data = qs.stringify(req);
    console.log('%c--- end ---', 'background:#000;color:#bada55');
    axios.post(url, data, { withCredentials: true }).then((res: any) => {
        console.log('%c--返回参数--', 'background:#000;color:#da00ff');
        var r: any = res.data;
        console.log(r);
        console.log('%c--- end ---', 'background:#000;color:#da00ff');
        if (r.code === 0 || r.code === -252) {
            func(r)
        } else {
            WLMessage('请求错误：' + r.msg, "warning")
        }
    }).catch((err) => {
        console.warn(err);
    });
};

//其他请求方式
export const AjaxPost = (url: string, req: MsgReqBase, func: any) => {
    // testjiazai();
    console.log('%c--请求参数--', 'background:#000;color:#bada55');
    console.log(req);
    // let data = qs.stringify(req);
    console.log('%c--- end ---', 'background:#000;color:#bada55');
    axios.post(url, req, { withCredentials: true }).then((res: any) => {
        console.log('%c--返回参数--', 'background:#000;color:#da00ff');
        var r: any = res.data;
        console.log("iserr", r.code);
        console.log('%c--- end ---', 'background:#000;color:#da00ff');
        if (r.code === 0) {
            func(r)
        } else if (r.code == -254 || r.code == -250) {
            console.log("登录超时", "")
            restart()
            router.push('/login');
        } else {
            WLMessage(r.msg, "warning")
        }
    }).catch((err) => {
        console.warn(err);
    });
};

function kkks() {

}

//查询登录请求方式
export const checkAjaxPost = (url: string, req: MsgReqBase, func: any) => {
    // testjiazai();
    console.log('%c--请求参数--', 'background:#000;color:#bada55');
    console.log(req);
    // let data = qs.stringify(req);
    console.log('%c--- end ---', 'background:#000;color:#bada55');
    axios.post(url, req, { withCredentials: true }).then((res: any) => {
        console.log('%c--返回参数--', 'background:#000;color:#da00ff');
        var r: any = res.data;
        console.log("iserr", r.code);
        console.log('%c--- end ---', 'background:#000;color:#da00ff');
        if (r.code === 0 || r.code == -255) {
            func(r)
        } else {
            WLMessage(r.msg, "warning")
        }
    }).catch((err) => {
        console.warn(err);
    });
};


//查询登录请求方式
export const authorityAjaxPost = (url: string, req: MsgReqBase, func: any) => {
    // testjiazai();
    console.log('%c--请求参数--', 'background:#000;color:#bada55');
    console.log(req);
    // let data = qs.stringify(req);
    console.log('%c--- end ---', 'background:#000;color:#bada55');
    axios.post(url, req, { withCredentials: true }).then((res: any) => {
        console.log('%c--返回参数--', 'background:#000;color:#da00ff');
        var r: any = res.data;
        console.log("iserr", r.code);
        console.log('%c--- end ---', 'background:#000;color:#da00ff');
        if (r.code === 0 || r.code == -253) {
            func(r)
        } else {
            WLMessage(r.msg, "warning")
        }
    }).catch((err) => {
        console.warn(err);
    });
};

export const urlHandler = function (uc: URL_MOCK) {
    /** 不是调试模式, 和该 url 不使用 mock 时候使用正式 url */
    if (!Config.debug || !uc.mock) {
        return Config.ajaxUrl + uc.url;
    } else {
        return uc.mockHost + uc.url;
    }
}

